package top.hkyzf.study.leetcode.string;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description: 
 * @author zhufeng
 * @date 2023/4/17 9:21
 * @version V1.0
 */
public class 统计共同度过的日子数_2409 {
    @Test
    public void testCountDaysTogether () {
        String arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19";
        int i = countDaysTogether(arriveAlice, leaveAlice, arriveBob, leaveBob);
        System.out.println(i);
    }

    public int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
        int arriveAliceDay = strToInt(arriveAlice);
        int arriveBobDay = strToInt(arriveBob);
        int leaveAliceDay = strToInt(leaveAlice);
        int leaveBobDay = strToInt(leaveBob);
        int arrive = arriveBobDay;
        int leave = leaveBobDay;
        if (arriveAliceDay > arriveBobDay) {
            arrive = arriveAliceDay;
        }
        if (leaveAliceDay < leaveBobDay) {
            leave = leaveAliceDay;
        }
        int rtn = leave - arrive + 1;
        if (rtn < 1) {
            return 0;
        } else {
            return rtn;
        }
    }

    private int strToInt(String date) {
        int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        List<Integer> dateInt = Arrays.stream(date.split("-")).map(Integer::parseInt).collect(Collectors.toList());
        int result = 0;
        for (int i = 0; i < dateInt.get(0)-1; i++) {
            result += monthDays[i];
        }
        return result + dateInt.get(1);
    }
}
